var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/modal/index.tsx
var modal_exports = {};
__export(modal_exports, {
  InjectModalCtx: () => InjectModalCtx,
  InnerModal: () => InnerModal,
  Modal: () => Modal,
  ModalContext: () => ModalContext,
  ModalHeader: () => ModalHeader,
  showAsyncModal: () => showAsyncModal,
  showConfirm: () => showConfirm,
  showModal: () => showModal,
  useModal: () => useModal
});
module.exports = __toCommonJS(modal_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_index = require("./index.less");
var import_react_resizable = require("react-resizable");
var import_icon = require("../../icon");
var import_util = require("../../util");
var import_widgets = require("../../widgets");
var import_Button = require("../antd/Button");
var destroyFns = /* @__PURE__ */ new Set();
var ModalContext = (0, import_react.createContext)({ ins: {}, params: {} });
var useModal = () => {
  const ctx = (0, import_react.useContext)(ModalContext);
  return [ctx.ins, ctx];
};
var injectModal = null;
var InjectModalCtx = import_react.default.memo(() => {
  const [modal, contextHolder] = import_antd.Modal.useModal();
  injectModal = modal;
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, contextHolder);
});
var modalDefaultHeader = {
  className: "ant-modal-header iup-title-tip",
  style: {
    alignItems: "center",
    padding: "0 var(--modal-header-padding-horizontal) 0 0",
    display: "flex",
    position: "relative",
    cursor: "move",
    height: "var(--modal-header-height, 40)",
    backgroundColor: "var(--modal-header-bg)"
  }
};
function ModalHeader({
  title,
  closable,
  fullscreenable,
  close,
  children
}) {
  const [, ctx] = useModal();
  const elRef = (0, import_react.useRef)();
  const [full, setFull] = (0, import_react.useState)(false);
  const setTransform = (0, import_util.useDraggable)(elRef, {
    shouldCancel: (e) => full || !!import_util.iup.closest(e.target, (el) => el.className === "disabled-draggable")
  });
  (0, import_util.useUpdateEffect)(() => {
    setTransform && setTransform(import_util.iup.closest(elRef.current, (el) => el.className === "ant-modal-content"));
  }, [setTransform]);
  (0, import_util.useUpdateEffect)(() => {
    var _a, _b;
    (_b = (_a = ctx.ins.getApi()) == null ? void 0 : _a.setFullscreen) == null ? void 0 : _b.call(_a, full);
    setTransform(
      import_util.iup.closest(elRef.current, (el) => el.className === "ant-modal-content"),
      full ? { dx: 0, dy: 0 } : {}
    );
  }, [full]);
  if (import_util.iup.isReactElement(title)) {
    return /* @__PURE__ */ import_react.default.createElement("div", { ref: elRef }, title);
  }
  return /* @__PURE__ */ import_react.default.createElement(import_util.Layout, { direction: "row", outRef: elRef, ...modalDefaultHeader }, /* @__PURE__ */ import_react.default.createElement("span", { className: "iup-modal-title" }, title), /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "iup-modal-title-children" }, import_util.iup.isReactElement(children) ? import_react.default.cloneElement(children, { className: "disabled-draggable" }) : children)), fullscreenable && /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "header-right-icon disabled-draggable",
      title: full ? "退出全屏" : "全屏",
      onClick: () => setFull(!full)
    },
    full ? /* @__PURE__ */ import_react.default.createElement(import_icons.ShrinkOutlined, null) : /* @__PURE__ */ import_react.default.createElement(import_icons.ArrowsAltOutlined, null)
  ), closable && /* @__PURE__ */ import_react.default.createElement("div", { className: "header-right-icon disabled-draggable", title: "关闭", onClick: close }, /* @__PURE__ */ import_react.default.createElement(import_icons.CloseOutlined, null)));
}
function Footer({ footer }) {
  const ctx = (0, import_react.useContext)(ModalContext);
  const {
    children = null,
    okText = "确定",
    cancelText = "取消",
    okButtonProps = {},
    cancelButtonProps = {},
    leftNode,
    rightNode
  } = footer || {};
  if (children === false) {
    return null;
  }
  if (children && children !== true) {
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, children);
  }
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        display: "flex",
        alignItems: "center",
        justifyContent: "flex-end",
        padding: `${ctx.size === "large" ? 24 : 16}px`
      }
    },
    leftNode,
    cancelText && /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        ...cancelButtonProps,
        style: { marginLeft: 8, minWidth: 68, ...cancelButtonProps.style },
        onClick: ctx.cancelClick
      },
      cancelText
    ),
    okText && /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        type: "primary",
        ...okButtonProps,
        style: { marginLeft: 8, minWidth: 68, ...okButtonProps.style },
        onClick: ctx.okClick
      },
      okText
    ),
    rightNode
  );
}
function ModalContent({
  ins,
  size: sizeProp,
  title,
  contentStyle,
  children,
  footer,
  contentIcon,
  closable,
  fullscreen,
  params
}) {
  const { onOk, onCancel } = footer || {};
  const providerValue = (0, import_react.useMemo)(() => {
    var _a, _b;
    return {
      ins,
      params,
      size: sizeProp,
      // 水印
      waterMark: (_b = (_a = import_util.iup.external).getWaterMark) == null ? void 0 : _b.call(_a),
      // 确定事件
      okClick: async () => {
        const args = await ins.notify(ins, "onOk");
        await (onOk == null ? void 0 : onOk(ins, args));
      },
      // 取消事件
      cancelClick: async () => {
        const args = await ins.notify(ins, "onCancel");
        if (onCancel) {
          await onCancel(ins, args);
        } else {
          ins == null ? void 0 : ins.destroy();
        }
      }
    };
  }, [ins, params, onOk, onCancel]);
  const divRef = (0, import_react.useRef)(null);
  const [size, setSize] = (0, import_react.useState)({ width: params.width, height: params.height });
  const [dragSize, setDragSize] = (0, import_react.useState)({
    position: "absolute",
    backgroundColor: "#000",
    pointerEvents: "none",
    width: params.width,
    height: params.height,
    zIndex: 2,
    opacity: 0.15,
    display: "none",
    top: 0,
    left: 0
  });
  const onResize = (e, opt) => {
    setDragSize((prevState) => ({ ...prevState, width: opt.size.width, height: opt.size.height }));
  };
  const onResizeStart = (e, opt) => {
    setDragSize((prevState) => ({
      ...prevState,
      display: "block",
      width: opt.size.width,
      height: opt.size.height
    }));
  };
  (0, import_util.useUpdateEffect)(() => {
    (0, import_widgets.polyfillResize)();
  }, [size]);
  const onResizeStop = (e, opt) => {
    import_util.iup.batchedUpdates(() => {
      setDragSize((prevState) => ({
        ...prevState,
        display: "none",
        width: opt.size.width,
        height: opt.size.height
      }));
      setSize(opt.size);
      ins.update({
        width: opt.size.width
      });
    });
  };
  (0, import_react.useEffect)(() => {
    var _a;
    if (params.height > 0) {
      const newHeight = (_a = divRef.current) == null ? void 0 : _a.clientHeight;
      if (newHeight && size.height !== newHeight) {
        setSize({ width: params.width, height: newHeight });
      }
    }
  }, []);
  const canResize = size.height !== "auto";
  const content = (style) => /* @__PURE__ */ import_react.default.createElement(ModalContext.Provider, { value: providerValue }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout, { style: { ...style } }, title && /* @__PURE__ */ import_react.default.createElement(
    ModalHeader,
    {
      title,
      closable: !!closable,
      fullscreenable: canResize ? !!fullscreen : false,
      close: providerValue.cancelClick
    }
  ), /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, { direction: "row", style: contentStyle }, (contentIcon == null ? void 0 : contentIcon.type) ? import_react.default.createElement(import_icon.IUPIcon[contentIcon == null ? void 0 : contentIcon.type], {
    style: {
      fontSize: 22,
      alignSelf: "start",
      marginRight: 8,
      color: import_util.cssVar.primaryColor,
      ...contentIcon == null ? void 0 : contentIcon.style
    }
  }) : null, /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, children)), footer && /* @__PURE__ */ import_react.default.createElement(Footer, { footer })), providerValue.waterMark);
  if (!canResize) {
    return content(size);
  } else {
    const { clientWidth, clientHeight } = document.documentElement || document.body;
    ins.setApi({
      setFullscreen(fullscreen2 = true) {
        if (fullscreen2) {
          import_util.iup.batchedUpdates(() => {
            setSize({ width: "100vw", height: "100vh" });
            ins.update({
              width: "100vw",
              style: { marginTop: 0, maxWidth: "100vw", position: "fixed", left: 0, top: 0 }
            });
          });
        } else {
          import_util.iup.batchedUpdates(() => {
            setSize({ width: dragSize.width, height: dragSize.height });
            ins.update({
              width: dragSize.width,
              style: { marginTop: -50, position: "relative", left: "unset", top: "unset" }
            });
          });
        }
        return fullscreen2;
      }
    });
    return /* @__PURE__ */ import_react.default.createElement(
      import_react_resizable.Resizable,
      {
        width: dragSize.width,
        height: dragSize.height,
        minConstraints: [params.width / 2 || 100, params.height / 2 || 80],
        maxConstraints: [clientWidth - 40, clientHeight - 130],
        onResizeStart,
        onResizeStop,
        onResize
      },
      /* @__PURE__ */ import_react.default.createElement("div", { ref: divRef }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            ...size,
            zIndex: 1,
            maxWidth: "100%",
            maxHeight: "100%",
            pointerEvents: `${dragSize.display === "block" ? "none" : "initial"}`
          }
        },
        content()
      ), /* @__PURE__ */ import_react.default.createElement("div", { style: dragSize }))
    );
  }
}
function showModal(allProps) {
  const {
    width,
    height,
    footer,
    footerLeft,
    footerRight,
    title,
    params,
    useModal: useModal2 = true,
    content,
    getContainer,
    closable = true,
    fullscreen = true,
    okText,
    cancelText,
    onOk,
    onCancel,
    okButtonProps = {},
    cancelButtonProps = {},
    afterClose: defaultAfterClose,
    contentStyle,
    className,
    type,
    icon,
    contentIcon,
    size = "large",
    ...props
  } = allProps;
  const api = {};
  const observer = new import_util.Observer();
  const modalObj = useModal2 ? import_util.iup.isBoolean(useModal2) ? injectModal || import_antd.Modal : useModal2 : import_antd.Modal;
  const afterClose = () => {
    defaultAfterClose == null ? void 0 : defaultAfterClose();
  };
  const ins = {
    ...modalObj.info({
      width: width || "auto",
      className: import_util.iup.classNames(className, "iup-ant-modal"),
      icon: null,
      getContainer: getContainer ?? (0, import_util.getGlobalContainer)(),
      destroyOnClose: true,
      maskClosable: false,
      keyboard: false,
      title: null,
      footer: null,
      centered: true,
      zIndex: 1001,
      afterClose: () => {
        observer.clear();
        afterClose == null ? void 0 : afterClose();
      },
      content: null,
      styles: { body: { padding: 0 }, content: { padding: 0 } },
      ...props
    }),
    getApi: () => api,
    setApi: (events) => import_util.iup.assign(api, events),
    subscribe: (...args) => {
      return observer.subscribe(...args);
    },
    notify: (...args) => {
      return observer.notify(...args);
    }
  };
  if (modalObj !== import_antd.Modal) {
    const oldDestroy = ins.destroy;
    ins.destroy = (...args) => {
      destroyFns.delete(ins.destroy);
      ins.notify([args], "beforeDestroy");
      oldDestroy(...args);
      observer.clear();
    };
    destroyFns.add(ins.destroy);
  }
  const updateContent = (c, newOptions = {}) => {
    ins.update({
      ...newOptions,
      content: /* @__PURE__ */ import_react.default.createElement(
        ModalContent,
        {
          ins,
          title,
          size,
          contentIcon,
          closable,
          fullscreen,
          contentStyle,
          footer: {
            children: footer,
            leftNode: footerLeft,
            rightNode: footerRight,
            okText,
            cancelText,
            okButtonProps,
            cancelButtonProps,
            onOk,
            onCancel
          },
          params: { ...params, width: width || 600, height: height || "auto" }
        },
        c
      )
    });
  };
  updateContent(content);
  ins.updateContent = updateContent;
  return ins;
}
function showAsyncModal(allProps) {
  const { beforeResolve, autoDestroy = false, ...params } = allProps;
  return new Promise((resolve) => {
    showModal({
      ...params,
      onOk: async (ins) => {
        if (await (beforeResolve == null ? void 0 : beforeResolve(true)) !== false) {
          resolve([true, ins]);
          autoDestroy && ins.destroy();
        }
      },
      onCancel: async (ins) => {
        if (await (beforeResolve == null ? void 0 : beforeResolve(false)) !== false) {
          resolve([false, ins]);
          autoDestroy && ins.destroy();
        }
      }
    });
  });
}
function showConfirm(props) {
  return new Promise((resolve) => {
    (0, import_util.getAntApp)().modal.confirm({
      ...props,
      onOk() {
        resolve(true);
      },
      onCancel() {
        resolve(false);
      },
      afterClose() {
        var _a;
        (_a = props.afterClose) == null ? void 0 : _a.call(props);
      }
    });
  });
}
var oldDestroyAll = import_antd.Modal.destroyAll;
import_antd.Modal.destroyAll = () => {
  oldDestroyAll();
  destroyFns.forEach((close) => {
    close == null ? void 0 : close();
  });
};
function InnerModal({ styles = {}, bodyStyle, ...props }) {
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      centered: true,
      zIndex: 1001,
      getContainer: (0, import_util.getGlobalContainer)(),
      ...props,
      styles: { ...styles, body: { ...styles.body, ...bodyStyle } }
    }
  );
}
var Modal = InnerModal;
Object.keys(import_antd.Modal).forEach((key) => {
  if (!Modal.hasOwnProperty(key)) {
    Object.defineProperty(Modal, key, {
      get() {
        var _a, _b;
        return ((_b = (_a = (0, import_util.getAntApp)()) == null ? void 0 : _a.modal) == null ? void 0 : _b[key]) || import_antd.Modal[key];
      }
    });
  }
});
import_util.iup.registerExternal({ showModal, showConfirm, showAsyncModal });
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  InjectModalCtx,
  InnerModal,
  Modal,
  ModalContext,
  ModalHeader,
  showAsyncModal,
  showConfirm,
  showModal,
  useModal
});
